#include "driver.h"

status driver::pressed(struct wiimote_t* wm){
    if(IS_PRESSED(wm, WIIMOTE_BUTTON_A)) return 1;
    else if(IS_PRESSED(wm, WIIMOTE_BUTTON_B)) return 2;
    else if(IS_PRESSED(wm, WIIMOTE_BUTTON_ONE)) return 3;
    else if(IS_PRESSED(wm, WIIMOTE_BUTTON_TWO)) return 4;
    else if(IS_PRESSED(wm, WIIMOTE_BUTTON_PLUS)) return 5;
    else if(IS_PRESSED(wm, WIIMOTE_BUTTON_MINUS)) return 6;
    else if(IS_PRESSED(wm, WIIMOTE_BUTTON_HOME)) return 7;
    else if(IS_PRESSED(wm, WIIMOTE_BUTTON_UP)) return 8;
    else if(IS_PRESSED(wm, WIIMOTE_BUTTON_DOWN)) return 9;
    else if(IS_PRESSED(wm, WIIMOTE_BUTTON_LEFT)) return 10;
    else if(IS_PRESSED(wm, WIIMOTE_BUTTON_RIGHT)) return 11;
    //else if(IS_PRESSED(wm, WIIMOTE_BUTTON_ALL)) return true;

    return 0;
}

status driver::released (struct wiimote_t *wm){
    if(IS_RELEASED(wm, WIIMOTE_BUTTON_A)) return 1;
    else if(IS_RELEASED(wm, WIIMOTE_BUTTON_B)) return 2;
    else if(IS_RELEASED(wm, WIIMOTE_BUTTON_ONE)) return 3;
    else if(IS_RELEASED(wm, WIIMOTE_BUTTON_TWO)) return 4;
    else if(IS_RELEASED(wm, WIIMOTE_BUTTON_PLUS)) return 5;
    else if(IS_RELEASED(wm, WIIMOTE_BUTTON_MINUS)) return 6;
    else if(IS_RELEASED(wm, WIIMOTE_BUTTON_HOME)) return 7;
    else if(IS_RELEASED(wm, WIIMOTE_BUTTON_UP)) return 8;
    else if(IS_RELEASED(wm, WIIMOTE_BUTTON_DOWN)) return 9;
    else if(IS_RELEASED(wm, WIIMOTE_BUTTON_LEFT)) return 10;
    else if(IS_RELEASED(wm, WIIMOTE_BUTTON_RIGHT)) return 11;

    return 0;
}


void driver::handle_event(struct wiimote_t* wm){
    uint *opt = new uint;
    memset(&ev, 0 , sizeof(ev));
    status press = pressed(wm);
    status releas = released(wm);

    if(press || releas){
        ev.type = EV_KEY;
        if(press) ev.value = 1;
        else ev.value = 0;
        switch((press ? press : releas)){
            case 1:
                *opt = BTN_LEFT; break;
            case 2:
                *opt = BTN_RIGHT; break;
            case 3:
                *opt = KEY_VOLUMEUP; break;
            case 4:
                *opt = KEY_VOLUMEDOWN; break;
            case 5:
                *opt = KEY_NEXTSONG; break;
            case 6:
                *opt = KEY_PREVIOUSSONG; break;
        /*     case 7:
                ev.code =
            case 8:
                ev.code =
                */
        }
        ev.code = *opt;
        if(write(fd, &ev, sizeof(ev)) < 0 ){std::cerr << "error: write action" << std:: endl; }
    }
    else{
        if(WIIUSE_USING_ACC(wm)){
            memset(&xy, 0, sizeof(struct input_event));
            xy[0].type = EV_ABS;
            xy[0].code = ABS_X;
            xy[0].value = wm->ir.ax;
            xy[1].type = EV_ABS;
            xy[1].code = ABS_Y;
            xy[1].value = wm->ir.ay;

            std::cout << "el y es " << wm->ir.y << std::endl;
            std::cout << "la x es "<< wm->ir.x << std::endl;
            if(write(fd, &xy, sizeof(xy)) < 0){std::cerr << "error: write xy" << std::endl;}
        }
    }



    memset(&ev, 0, sizeof(ev));
    ev.type = EV_SYN;
    ev.code = 0;
    ev.value = 0;
    if(write(fd, &ev, sizeof(ev)) < 0){std::cerr << "error: sync" << std::endl; }

}
